using System.Collections.Generic;
using Gameplay.PVE.Entity;
using UnityEngine;
using Yoozoo.Libs;
using Yoozoo.Managers;

namespace Gameplay.PVE
{
    public class ControlBehaviour : IClass
    {
        private UnitBase unit;
        private List<SpecialActionBase> actions;

        void IClass.OnReset()
        {
            
        }

        public void Init(UnitBase unit)
        {
            this.unit = unit;
            actions = new List<SpecialActionBase>();
        }

        public void Update()
        {
            EControlType type = EControlType.None;
            for (int i = 0; i < actions.Count; i++)
            {
                actions[i].Update();
                /*if (actions[i].state == ESpecialActionState.None)
                {
                    ClassManager.Free(actions[i]);
                    actions.RemoveAt(i);
                    i--;
                }
                else*/
                {
                    if ((int) type <= (int) actions[i].controlType)
                    {
                        type = actions[i].controlType;
                    }
                }
            }
            
            unit.Data.controlType = type;
        }

        public void AddControl(AddControlArg arg)
        {
            List<SpecialActionBase> actionList = new List<SpecialActionBase>();

            if (arg.controllType == (int)EControlType.Hit)
            {
                var action = ClassManager.Get<HitAction>();
                action.controlType = EControlType.Hit;
                actionList.Add(action);
            }
            else if (arg.controllType == (int)EControlType.KnockBack)
            {
                var action = ClassManager.Get<KnockBackAction>();
                action.controlType = EControlType.KnockBack;
                actionList.Add(action);
                for (int i = 0; i < actions.Count; i++)
                {
                    if (actions[i] is KnockBackAction)
                    {
                        actions[i].Free();
                        actions.RemoveAt(i);
                        i--;
                    }
                }
            }
            else if (arg.controllType == (int) EControlType.Drag)
            {
                var action = ClassManager.Get<DragAction>();
                action.controlType = EControlType.Drag;
                actionList.Add(action);
            }
            else if (arg.controllType == (int) EControlType.Stun)
            {
                var action = ClassManager.Get<StunAction>();
                action.controlType = EControlType.Stun;
                actionList.Add(action);
            }

            else if (arg.controllType == (int) EControlType.AttractAttack)
            {
                /*var targetTeam = unit.Data.GetTargetTeam(unit.Data.searchType);
                if (targetTeam == arg.attractAttackTarget.Data.team)
                {
                    PveFlyTextManager.Instance.ShowFlyText(string.Format("被{0}嘲讽", arg.attractAttackTarget.Data.id),
                        unit.Data.position + new Vector3(0, 0.5f, 0));
                }*/
                if (arg.attractAttackTarget != null)
                {
                    var action = ClassManager.Get<AttrackAttackAction>();
                    action.controlType = EControlType.AttractAttack;
                    actionList.Add(action);
                }
            }

            else if (arg.controllType == (int) EControlType.Silent)
            {
                /*PveFlyTextManager.Instance.ShowFlyText(string.Format("沉默"),
                    unit.Data.position + new Vector3(0, 0.5f, 0));*/
                var action = ClassManager.Get<SilentAction>();
                action.controlType = EControlType.Silent;
                actionList.Add(action);
            }

            else if (arg.controllType == (int) EControlType.Disarm)
            {
                /*PveFlyTextManager.Instance.ShowFlyText(string.Format("缴械"),
                    unit.Data.position + new Vector3(0, 0.5f, 0));*/
                var action = ClassManager.Get<DisarmAction>();
                action.controlType = EControlType.Disarm;
                actionList.Add(ClassManager.Get<DisarmAction>());
            }

            for (int i = 0; i < actionList.Count; i++)
            {
                var action = actionList[i];
                action.Init(unit as BattleUnit);
                action.Start(arg);
                actions.Add(action);
            }
        }

        public void RemoveControl(RemoveControllArg arg)
        {
            for (int i = 0; i < actions.Count; i++)
            {
                if (actions[i].element == arg.element)
                {
                    actions[i].Free();
                    ClassManager.Free(actions[i]);
                    actions.RemoveAt(i);
                    RefreshState();
                    break;
                }
            }
        }

        private void RefreshState()
        {
            bool isDisarm = false;
            bool isSilent = false;
            for (int i = 0; i < actions.Count; i++)
            {
                if (actions[i].controlType == EControlType.Disarm)
                {
                    isDisarm = true;
                }
                else if (actions[i].controlType == EControlType.Silent)
                {
                    isSilent = true;
                }
            }

            unit.Data.isDisarm = isDisarm;
            unit.Data.isSilent = isSilent;
        }
        
    }
}